package com.jyxd.web.controller.basic;

import com.jyxd.web.data.basic.RecordListItem;
import com.jyxd.web.data.user.User;
import com.jyxd.web.service.assessment.basic.RecordListItemService;
import com.jyxd.web.util.HttpCode;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/RecordListItem")
public class RecordListItemController {

    @Autowired
    private RecordListItemService recordListItemService;


    /**
     * 保存记录单数据
     *
     * @return
     */
    @RequestMapping(value = "/savedata")
    @ResponseBody
    public String savedata(@RequestBody Map<String, Object> map, HttpSession session) {
        User user = (User) session.getAttribute("user");
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "添加失败");
        JSONObject body =JSONObject.fromObject(map.get("body").toString());
        JSONObject recordList = body.getJSONObject("recordList");
        JSONArray recordListItem = body.getJSONArray("recordListItem");
        try {
            json = recordListItemService.savedate(recordList, recordListItem,user);
        } catch (Exception e) {

        }
        return json.toString();
    }

    /**
     * 根据条件分页查询异常生命体征表记录列表（也可以不分页）
     * @param map
     * @return
     */
    @RequestMapping(value = "/queryList",method= RequestMethod.POST)
    @ResponseBody
    public String queryList(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data",new ArrayList<>());
        json.put("msg","暂无数据");
        List<RecordListItem> list =recordListItemService.queryList(map);
        if(list!=null && list.size()>0){
            json.put("msg","查询成功");
            json.put("data", JSONArray.fromObject(list));
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }




}
